//
//  UIApplication+Extension.m
//
//  Created by PP on 16/1/6.
//  Copyright (c) 2016年 PP. All rights reserved.
//

#import "UIApplication+Extension.h"

@implementation UIApplication (Extension)

+ (UIWindow *)keyWindow
{
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (window.isKeyWindow)
        {
            return window;
        }
    }
    return [self firstWindow];
}

+ (UIWindow *)firstWindow
{
    return [[[UIApplication sharedApplication] windows] firstObject];
}

+ (UIViewController *)topViewController
{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    UIViewController* currentViewController = window.rootViewController;
    while (1) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            } else {
                break;
            }
        }
    }
    if ([currentViewController isKindOfClass:[RTContainerController class]]) {
        RTContainerController *tempVc = (RTContainerController *)currentViewController;
        return tempVc.contentViewController;
    }
    return currentViewController;
}
+ (UINavigationController *)topNaviViewController
{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    UIViewController* currentViewController = window.rootViewController;
    while (1) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                break;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                currentViewController = ((UITabBarController* )currentViewController).selectedViewController;
            }
        }
    }
    return (UINavigationController *)currentViewController;
}
+ (UITabBarController *)topTabbarController
{
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    NSAssert(window, @"The window is empty");
    UIViewController* currentViewController = window.rootViewController;
    while (1) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else {
            if ([currentViewController isKindOfClass:[UINavigationController class]]) {
                currentViewController = ((UINavigationController *)currentViewController).visibleViewController;
            } else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
                break;
            }
        }
    }
    return (UITabBarController *)currentViewController;
}
+ (UIViewController *)superViewController:(UIView *)view {
    UIResponder *responder = view;
    // 循环获取下一个响应者,直到响应者是一个UIViewController类的一个对象为止,然后返回该对象.
    while ((responder = [responder nextResponder])) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
    }
    return nil;
}
+ (BOOL)isPresentWith:(id)obj {
    if ([obj isKindOfClass:[UIView class]]) {
        if ([self superViewController:obj] == [self topViewController]) {
            return YES;
        } else {
            return NO;
        }
    } else if ([obj isKindOfClass:[UIViewController class]]) {
        if (obj == [self topViewController]) {
            return YES;
        } else {
            return NO;
        }
    } else {
        return NO;
    }
}
@end
